﻿using BBCore.Core.Models.Samples;
using BootstrapBlazor.Components;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using BBCore.Application.Extensions;

namespace BBCore.Server.Components.Pages.Samples
{
    /// <summary>
    /// 主从明细Crud导入导出示例
    /// </summary>
    public partial class HYJ_MasterDetail_Table_CRUD
    {
        private Table<HYJ_Detail_Table>? _Detail;
        private async Task<QueryData<HYJ_Master_Table>> OnMasterQueryAsync(QueryPageOptions option) => await IMasterDataQueryService.OnQueryAsync(option);
        private async Task<bool> OnMasterDeleteAsync(IEnumerable<HYJ_Master_Table> items)
        {
            var repo = freeSql.GetRepository<HYJ_Master_Table>();
            foreach (var item in items)
            {
                repo.DeleteCascadeByDatabase(a => a.HEAD_ID == item.HEAD_ID);
            }
            return await Task.FromResult(true);
        }
        private async Task<bool> OnMasterSaveAsync(HYJ_Master_Table item, ItemChangedType changedType)
        {
            if (changedType == ItemChangedType.Add)
            {
                await freeSql.Insert(item).ExecuteAffrowsAsync();
            }
            else if (changedType == ItemChangedType.Update)
            {
                await freeSql.Update<HYJ_Master_Table>().SetSource(item).ExecuteAffrowsAsync();
            }
            return await Task.FromResult(true);
        }

        private async Task<QueryData<HYJ_Detail_Table>> OnDetailQueryAsync(QueryPageOptions option, HYJ_Master_Table master)
        {
            return await IDetailDataQueryService.OnQueryAsync(option, q => q.Where(x => x.BODY_ID == master.HEAD_ID));
        }
        private async Task<bool> OnDetailDeleteAsync(IEnumerable<HYJ_Detail_Table> items)
        {
            await freeSql.Delete<HYJ_Detail_Table>().Where(items).ExecuteAffrowsAsync();

            return await Task.FromResult(true);
        }

        private async Task<bool> OnDetailSaveAsync(HYJ_Detail_Table item, ItemChangedType changedType, HYJ_Master_Table master)
        {
            item.BODY_ID = master.HEAD_ID;
            if (changedType == ItemChangedType.Add)
            {
                await freeSql.Insert(item).ExecuteAffrowsAsync();

            }
            else if (changedType == ItemChangedType.Update)
            {
                await freeSql.Update<HYJ_Detail_Table>().SetSource(item).ExecuteAffrowsAsync();
            }
            return await Task.FromResult(true);
        }
        private async Task tempDownload()
        {
            var filepath = await IExporterAndImporterService.TemplateDownloadAsync<HYJ_Detail_Table>();
            await DownloadFromFileAsync(filepath);
            await ShowToastMessageAsync("模板已下载", ToastCategory.Success);
        }
        /// <summary>
        /// 从表数据导入
        /// </summary>
        /// <param name="uploadFile">上传的数据</param>
        /// <param name="master">主表</param>
        /// <returns></returns>
        private async Task DataImport(UploadFile uploadFile, HYJ_Master_Table master)
        {
            await Data_Import<HYJ_Master_Table, HYJ_Detail_Table, int>(uploadFile, master);
            await _Detail!.QueryAsync();
        }
    }
}
